<?php

namespace app\admin\controller\info;

use app\admin\controller\Base;
use app\common\info\ArticleCategoryModel;
use app\common\info\ArticleModel;
use app\common\model\auth\AuthAdminModel;
use app\common\model\FileModel;

class Article extends Base
{
    /**
     * 列表
     */
    public function list()
    {
        $params = request()->only(['page', 'limit', 'field_type', 'field_value', 'status', 'category_id', 'start_time', 'end_time']);
        if (isset($params['field_type']) && !empty($params['field_type']) && isset($params['field_value']) && !empty($params['field_value'])) {
            $where[] = [$params['field_type'], 'like', "%{$params['field_value']}%"];
        }

        if (isset($params['status']) && !empty($params['status'])) {
            $where[] = ['status', 'in', explode(',', $params['status'])];
        }
        if (isset($params['category_id']) && !empty($params['category_id'])) {
            $where[] = ['category_id', 'in', explode(',', $params['category_id'])];
        }

        $list = (new ArticleModel)->where($where ?? [])
            ->order(['sort' => 'DESC', 'id' => 'DESC'])
            ->field('id,title,category_id,author,sort,cover_file_id,status,create_time,update_time')
            ->paginate([
                'list_rows' => $params['limit'] ?? 10,
                'page' => $params['page'] ?? 1
            ]);
        $categoryData = (new ArticleCategoryModel)->whereIn('id', array_column($list->items(), 'category_id'))->column('id,name', 'id');
        foreach ($list->items() as &$v) {
            $v['status_attr'] = (new ArticleModel)->status[$v['status']];
            $v['category_name'] = $categoryData[$v['category_id']]['name'] ?? '';
            $v['cover_file_info'] = (new FileModel)->getFileByInfo($v['cover_file_id']);
        }

        return response_success('', [
            'count' => $list->total(),
            'data' => $list->items()
        ]);
    }

    /**
     * 添加
     */
    public function add()
    {
        $params = request()->only(['title', 'category_id', 'author', 'sort', 'synopsis', 'cover_file_id', 'status', 'content']);
        $this->validate($params, [
            'title|标题' => 'require',
            'category_id|分类' => 'require',
            'synopsis|简介' => 'require',
            'cover_file_id|封面' => 'require',
            'status|状态' => 'require|in:1,2',
            'content|内容' => 'require'
        ], [
            'status.in' => '状态枚举值仅限1,2'
        ]);

        if ((new ArticleModel)->where('title', $params['title'])->find()) {
            return response_error('文章标题已存在');
        }

        (new ArticleModel)->insert([
            'title' => $params['title'],
            'category_id' => $params['category_id'],
            'author' => $params['author'],
            'sort' => $params['sort'],
            'synopsis' => $params['synopsis'],
            'cover_file_id' => $params['cover_file_id'],
            'status' => $params['status'],
            'content' => $params['content'],
            'create_admin_id' => $this->adminInfo['id'],
            'create_time' => date('Y-m-d H:i:s')
        ]);

        return response_success();
    }

    /**
     * 删除
     */
    public function del()
    {
        $params = request()->only(['ids']);
        $this->validate($params, [
            'ids' => 'require'
        ], [
            'ids.require' => '请选择删除文章'
        ]);

        $articleModel = new ArticleModel;
        if (!$articleModel->delArticle($params['ids'])) {
            return response_error($articleModel->getError());
        }

        return response_success();
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $params = request()->only(['id', 'title', 'category_id', 'author', 'sort', 'synopsis', 'cover_file_id', 'status', 'content']);
        $this->validate($params, [
            'id|文章ID' => 'require',
            'title|标题' => 'require',
            'category_id|分类' => 'require',
            'synopsis|简介' => 'require',
            'cover_file_id|封面' => 'require',
            'status|状态' => 'require|in:1,2',
            'content|内容' => 'require'
        ], [
            'status.in' => '状态枚举值仅限1,2'
        ]);

        if ((new ArticleModel)->where('id', '<>', $params['id'])->where('title', $params['title'])->find()) {
            return response_error('文章标题已存在');
        }

        (new ArticleModel)->where('id', $params['id'])->update([
            'title' => $params['title'],
            'category_id' => $params['category_id'],
            'author' => $params['author'],
            'sort' => $params['sort'],
            'synopsis' => $params['synopsis'],
            'cover_file_id' => $params['cover_file_id'],
            'status' => $params['status'],
            'content' => $params['content'],
            'update_time' => date('Y-m-d H:i:s')
        ]);

        return response_success();
    }

    /**
     * 获取详情
     */
    public function getInfo()
    {
        $params = request()->only(['id']);
        $this->validate($params, [
            'id|文章ID' => 'require'
        ]);

        $info = (new ArticleModel)->where('id', $params['id'])->find();
        if (!$info) {
            return response_error('文章信息不存在或已删除');
        }
        $info['create_admin_name'] = (new AuthAdminModel)->getNamesByIds($info['create_admin_id']);
        $info['cover_file_info'] = (new FileModel)->getFileByInfo($info['cover_file_id']);

        return response_success('', $info);
    }

}